package com.egov.wrdmp.reporting.service.impl;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;

import org.pentaho.reporting.engine.classic.core.MasterReport;
import org.pentaho.reporting.engine.classic.core.ClassicEngineBoot;
import org.pentaho.reporting.engine.classic.core.modules.output.pageable.pdf.PdfReportUtil;
import org.pentaho.reporting.libraries.resourceloader.Resource;
import org.pentaho.reporting.libraries.resourceloader.ResourceException;
import org.pentaho.reporting.libraries.resourceloader.ResourceManager;
import org.springframework.stereotype.Service;

import com.egov.wrdmp.reporting.service.PentahoReportService;
@Service
public class PentahoReportServiceImpl implements PentahoReportService{

	public void generateReport(HttpServletRequest request,
			HttpServletResponse response, Map paramMap) {
		ClassicEngineBoot.getInstance().start();
		try {
			 // load report definition
			 ResourceManager manager = new ResourceManager();
			 manager.registerDefaults();
			 String reportPath = "file:///home/abhijeet/workspaces/workspace/ReportingEngine/templates/demo.prpt";
			 Resource res = null;
			try {
				res = manager.createDirectly(new URL(reportPath),MasterReport.class);
			} catch (MalformedURLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			 MasterReport report = (MasterReport) res.getResource();
			// render the pdf
			 response.setContentType("application/pdf");
			 try {
				PdfReportUtil.createPDF(report, response.getOutputStream());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (ResourceException e) {
			 e.printStackTrace();
		}
		
	}

	

}
